package com.base.vistter.iframe.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.base.vistter.iframe.entity.SysDepart;
import com.base.vistter.iframe.entity.SysUser;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.mapper.SysDepartMapper;
import com.base.vistter.iframe.mapper.SysUserMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class SysDepartService extends BaseService<SysDepartMapper, SysDepart> {

    @Resource(name = "sysUserMapper")
    private SysUserMapper sysUserMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean save(SysDepart entity) throws IframeException {
        boolean result = super.save(entity);
        if (entity.getParentId() == null) {
            entity.setParentPath(String.valueOf(entity.getId()));
        } else {
            SysDepart parent = super.getById(entity.getParentId());
            entity.setParentPath(parent.getParentPath() + PARENT_PATH_DIVIDE + entity.getId());
        }
        super.updateById(entity);
        return result;
    }

    @Transactional(readOnly = true)
    public SysDepart getById(Serializable id) throws IframeException {
        SysDepart sysDepart = super.getById(id);
        if (sysDepart.getLeader() != null) {
            SysUser sysUser = sysUserMapper.selectById(sysDepart.getLeader());
            if(sysUser != null){
                sysDepart.setLeaderName(sysUser.getRealname());
            }
        }
        return sysDepart;
    }

    /**
     * 更新时更新所有子节点
     * @param entity
     * @return
     * @throws IframeException
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateById(SysDepart entity) throws IframeException {
        SysDepart persistence = super.getById(entity.getId());
        if (entity.getParentId() == null) {
            entity.setParentPath(String.valueOf(entity.getId()));
            //如果如果父节点路径发生变化
            if (!Objects.equals(persistence.getParentId(), entity.getParentId())) {
                this.updateChildParentPath(persistence.getParentPath(), entity);
            }
        } else {
            SysDepart parent = super.getById(entity.getParentId());
            entity.setParentPath(parent.getParentPath() + PARENT_PATH_DIVIDE + entity.getId());
            //如果如果父节点路径发生变化
            if (!Objects.equals(persistence.getParentId(), entity.getParentId())) {
                this.updateChildParentPath(persistence.getParentPath(), entity);
            }
        }
        return super.updateById(entity);
    }

    private void updateChildParentPath(String parentPath, SysDepart sysDepart) throws IframeException {
        this.baseMapper.updateParentPath(parentPath, sysDepart.getParentPath());
    }

    public List<SysDepart> getChildren(Integer departId) throws IframeException{
        LambdaQueryWrapper<SysDepart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDepart::getParentId , departId);
        return super.selectList(lambdaQueryWrapper);
    }

    @Transactional(readOnly = true)
    public List<Integer> getRootChildrenIds(Integer departId) throws IframeException {
        SysDepart sysDepart = super.getById(departId);
        LambdaQueryWrapper<SysDepart> lambdaQueryWrapper = new LambdaQueryWrapper<SysDepart>();
        //本部门
        lambdaQueryWrapper.likeRight(SysDepart::getParentPath, sysDepart.getParentPath());
        List<SysDepart> list = super.selectList(lambdaQueryWrapper);
        return list.stream().map(item -> item.getId()).collect(Collectors.toList());
    }

}
